/**
 * huize Service Inc
 * All Rights Reserved @2018
 */
package com.huize.zhike.framework.common.enumeration;

import com.huize.ladon.apm.common.enums.EnumInterface;

/**
 * 描述: 操作结果枚举
 *
 * @author tianyuliang
 * @version $Id: OperationStatusEnum.java, v0.1 2019/3/25
 */
public enum OperationStatusEnum implements EnumInterface {

    /**
     * 操作成功
     */
    SUCCESSFUL(true, 1, "成功"),

    /**
     * 操作失败
     */
    FAILED(false, 0, "失败");

    private Boolean value;

    private Integer status;

    private String desc;

    OperationStatusEnum(Boolean value, Integer status, String desc) {
        this.value = value;
        this.status = status;
        this.desc = desc;
    }

    public static OperationStatusEnum of(Integer status) {
        if (status == null) {
            return FAILED;
        }

        for (OperationStatusEnum statusEnum : OperationStatusEnum.values()) {
            if (statusEnum.status.intValue() == status.intValue()) {
                return statusEnum;
            }
        }
        return FAILED;
    }

    public static OperationStatusEnum of(Boolean value) {
        if (value == null) {
            return FAILED;
        }
        for (OperationStatusEnum statusEnum : OperationStatusEnum.values()) {
            if (statusEnum.value.equals(value)) {
                return statusEnum;
            }
        }
        return FAILED;
    }

    public static Boolean isFailed(OperationStatusEnum statusEnum) {
        return statusEnum != null && statusEnum == FAILED;
    }

    public static Boolean isSuccessful(Integer status) {
        return status != null && status.intValue() == SUCCESSFUL.getStatus().intValue();
    }

    public static Boolean isSuccessful(OperationStatusEnum statusEnum) {
        return statusEnum != null && statusEnum == SUCCESSFUL;
    }

    public Boolean getValue() {
        return value;
    }

    public Integer getStatus() {
        return status;
    }

    @Override
    public String getCode() {
        return this.status.toString();
    }

    @Override
    public String getDesc() {
        return this.desc;
    }

    @Override
    public String toString() {
        return String.format("OperationStatusEnum{value=%s, desc=%s}", getValue(), getDesc());
    }
}
